package de.hauschild.gmltracer;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import de.hauschild.gmltracer.gml.Environment;
import de.hauschild.gmltracer.gml.Token;
import de.hauschild.gmltracer.gml.TokenStack;
import de.hauschild.gmltracer.gml.impl.GmlInterpreter;
import de.hauschild.gmltracer.gml.impl.GmlParser;

public class GmlTracer {

  private static final GmlInterpreter GML_INTERPRETER = new GmlInterpreter();

  public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
      System.err.println("No file to render specified.");
      System.exit(1);
    }

    final File file = new File(args[0]);
    if (!file.exists()) {
      System.err.println(String.format("Specified file '%s' could not be found.", file));
      System.exit(2);
    }

    final String program = FileUtils.readFileToString(file);
    final Environment environment = new Environment();
    final TokenStack tokenStack = new TokenStack();
    final List<Token> tokenSequence = new GmlParser().parse(program);
    GML_INTERPRETER.interpret(environment, tokenStack, tokenSequence);
  }
}